package com.jianyun.jyzs.model;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.jianyun.jyzs.bean.NewSignReslut;
import com.jianyun.jyzs.bean.SignNewReslut;
import com.jianyun.jyzs.bean.SignRange;
import com.jianyun.jyzs.bean.SignResult;
import com.jianyun.jyzs.bean.SignStateBean;
import com.jianyun.jyzs.bean.WillSignBean;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.INewSignModel;
import com.jianyun.jyzs.utils.LoginCache;

import java.util.HashMap;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/6/11.
 */

public class NewSignModel implements INewSignModel {

    private static NewSignModel model;
    private final String serviceUrl;

    private NewSignModel() {
        serviceUrl = LoginCache.getInstance().getSignVersion().getServiceUrl();
    }

    public static NewSignModel getInstance() {
        if (model == null) {
            model = new NewSignModel();
        }
        return model;

    }

    /**
     * 判断当前位置是否在签到范围
     *
     * @param enterpriseCode
     * @param lat
     * @param lng
     * @param onisLocationRangeListener
     */
    @Override
    public void judegLoactionRange(String enterpriseCode, String lat, String lng, final OnisLocationRangeListener onisLocationRangeListener) {
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);
        api.isSignFw(serviceUrl,"GetNearestAddress", enterpriseCode, lat, lng)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<SignRange>() {
                    @Override
                    protected void onResponse(SignRange response) {
                        Log.i("test", "范围：" + response.toString());
                        if (response.isResult()) {
                            onisLocationRangeListener.onRangeReslut(response);

                        } else {
                            onisLocationRangeListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onisLocationRangeListener.onFailed(msg);
                    }
                });
    }

    /**
     * 获取将要打卡的详细信息
     *
     * @param enterpriseCode
     * @param userId
     * @param onGetNowSignDeatileListener
     */
    @Override
    public void getNowSignDetail(String enterpriseCode, String userId, final OnGetNowSignDeatileListener onGetNowSignDeatileListener) {
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);

        Log.i("test","userid:"+userId+"\n"+enterpriseCode);
        api.getWillSignDetail(serviceUrl,"GetUserDKInfo", enterpriseCode, userId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Object object) {

                        try {
                            String s = JSON.toJSONString(object);
                            Log.i("test","获取打卡详细信息："+s.toString());
                            WillSignBean willSignBean = JSON.parseObject(s, WillSignBean.class);
                            if (willSignBean.isResult()) {
                                onGetNowSignDeatileListener.onSuccess(willSignBean);
                            }else {
                                onGetNowSignDeatileListener.onFailed(willSignBean.getMessage());
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                            String message = e.getMessage();
                            onGetNowSignDeatileListener.onFailed(message);
                        }


                    }
                });
    }

    /**
     * 获取用户将要打卡后的状态
     *
     * @param enterpriseCode
     * @param userId
     * @param onGetNowStateListener
     */
    @Override
    public void getUserSignState(String enterpriseCode, String userId,String signType, final OnGetNowStateListener onGetNowStateListener) {
        //Log.i("test",userId);
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);
        api.getUserSignState(serviceUrl,"GetUserDKState", enterpriseCode, userId,signType)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<SignStateBean>() {
                    @Override
                    protected void onResponse(SignStateBean response) {
                        if (response.getResult()) {
                            onGetNowStateListener.onSuccess(response);
                        } else {
                            onGetNowStateListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onGetNowStateListener.onFailed(msg);
                    }
                });
    }

    /**
     * 新版打卡
     *
     * @param map
     * @param onNewSignListener
     */
    @Override
    public void newSign(HashMap<String, String> map, final OnNewSignListener onNewSignListener) {
        Api api = RetrofitHelper.builderNewSignHttp().create(Api.class);
        api.newSign(serviceUrl,map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<SignNewReslut>() {
                    @Override
                    protected void onResponse(SignNewReslut response) {
                        if (response.isResult()) {
                            onNewSignListener.onSuccess(response);
                        } else {
                            onNewSignListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onNewSignListener.onFailed(msg);
                    }
                });
    }


}
